package org.lboot.idmo.module.user;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lboot.idmo.module.role.SysRole;
import org.lboot.idmo.module.user.role.SysUserRoleService;
import org.lboot.jpa.jpa.AbstractJpaCurdExt;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author kindear
 */

@Slf4j
@Service
@AllArgsConstructor
public class SysUserServiceImpl extends AbstractJpaCurdExt<Long, SysUser, SysUserDAO> implements SysUserService {
    SysUserRoleService userRoleService;

    @Override
    public Optional<SysUser> getByMobileV1(String mobile) {
        List<SysUser> users =  dao.findByMobile(mobile);
        if (users.size() > 1){
            log.warn("存在多条重复手机号:{}",mobile);
        }
        if (!users.isEmpty()){
            return Optional.of(users.get(0));
        }

        return Optional.empty();
    }

    @Override
    public List<SysUser> getByMobile(String mobile) {
        List<SysUser> users =  dao.findByMobile(mobile);
        if (users.size() > 1){
            log.warn("存在多条重复手机号:{}",mobile);
        }
        return users;
    }

    @Override
    public Boolean isMobileExist(String mobile) {
        return !getByMobile(mobile).isEmpty();
    }

    @Override
    public Optional<SysUser> getByUsername(String username) {
        return dao.findByUsername(username);
    }

    @Override
    public List<SysUser> getByIdBatch(List<Long> ids) {
        return dao.findByIdIn(ids);
    }

    @Override
    public List<SysUser> batchUpdate(List<SysUser> users) {
        return dao.saveAll(users);
    }

    @Override
    public List<SysRole> getRoles(Long userId) {
        return userRoleService.getRoles(userId);
    }
}
